#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>

void sigHandler(int signal){
    printf("Recive signal %d, %s\n", signal, strsignal(signal));
}


unsigned int my_alarm(unsigned int seconds){
    struct itimerval new, old;
    if(getitimer(ITIMER_REAL, &old) == -1){
        printf("Error getitimer %d %s\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }

    if (seconds == 0)
        new.it_value.tv_usec = seconds;
    new.it_value.tv_sec = seconds;
    if(setitimer(ITIMER_REAL, &new, NULL) == -1){
        printf("Error setitimer %d %s\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }

    return old.it_value.tv_sec;
}

int main(){
    struct sigaction act;
    sigset_t mask;

    sigemptyset(&mask);
    act.sa_handler = sigHandler;
    if (sigaction(SIGALRM ,&act, NULL) == -1){
        printf("Error sigaction %d %s\n", errno, strerror(errno));
    }

    printf("Set alarm %d\n", my_alarm(8));
    pause();
    exit(EXIT_SUCCESS);

}

